#ifndef ENGINEWEBGPU_H
#define ENGINEWEBGPU_H
#ifdef __EMSCRIPTEN__
#include <webgpu/webgpu_cpp.h> // Emscripten's WebGPU header
#include <string>
#else
#include <GLFW/glfw3.h>
#include <dawn/webgpu_cpp.h> // Dawn's WebGPU header for non-Emscripten builds
#endif

template<typename T>
struct AsyncResult {
    T result = nullptr;
    wgpu::RequestAdapterStatus status;
    std::string message;
};

class EngineWebGPU {
public:
    EngineWebGPU();
    ~EngineWebGPU();
    void init();
    void loop(wgpu::RenderPipeline renderPipeline);
    void loopPC(wgpu::RenderPipeline renderPipeline);

    wgpu::ShaderModule createShaderModule(const char* shaderSource);
    wgpu::PipelineLayout createPipelineLayout();
    wgpu::RenderPipeline createRenderPipeline(
        wgpu::PipelineLayout pipelineLayout,
        wgpu::ShaderModule shaderModule
    );
private:
    void createInstance();
    void findAdapter();
    void createDevice();
    void createSurface();
    wgpu::TextureView getSuffaceTextureView();

    #ifndef __EMSCRIPTEN__
        GLFWwindow* mWindow;
    #endif
    wgpu::Instance mInstance;
    wgpu::Adapter mAdapter;
    wgpu::Device mDevice;
    wgpu::Surface mSurface;
    wgpu::Queue mQueue;

    wgpu::TextureFormat wgpuRTVFormats[2];
};
#endif
